package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.PurchaseOrder;
import com.bean.PurchaseOrderMaterial;
import com.bean.ReturnApply;
import com.bean.ReturnApplyMaterial;
import com.bean.Stock;
import com.bean.User;
import com.service.ReturnApplyMaterialService;
import com.service.ReturnApplyService;
import com.service.StockService;
import com.util.TimeUtil;

/**
 * @author author : 张鹏
 * @date createTime：2018年2月25日 下午5:48:22
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("returnApply")
public class ReturnApplyController {
	@Autowired
	private ReturnApplyService returnApplyService;

	@Autowired
	private ReturnApplyMaterialService returnApplyMaterialService;

	@Autowired
	private StockService stockService;

	private ReturnApply returnApply;
	private ReturnApplyMaterial returnApplyMaterial;

	@RequestMapping("getReturnApplyByStateAjax")
	@ResponseBody
	public List<ReturnApply> getReturnApplyByStateAjax(int state) {
		return returnApplyService.getReturnApplyByState(state);
	}

	@RequestMapping("getReturnApplyBySupplierIdAjax")
	@ResponseBody
	public List<ReturnApply> getReturnApplyBySupplierIdAjax(HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		
		return returnApplyService.getReturnApplyBySupplierId(userId);
	}

	@RequestMapping("getReturnApplyMaterialGroupByMaterialIdAjax")
	@ResponseBody
	public List<ReturnApplyMaterial> getReturnApplyMaterialGroupByMaterialIdAjax() {
		return returnApplyMaterialService
				.getReturnApplyMaterialGroupByMaterialId();
	}

	@RequestMapping("getReturnApplyByStateAndSupplierIdAjax")
	@ResponseBody
	public List<ReturnApply> getReturnApplyByStateAndSupplierIdAjax(int state,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		returnApply = new ReturnApply();
		returnApply.setState(state);
		returnApply.setSupplierId(userId);
		return returnApplyService
				.getReturnApplyByStateAndSupplierId(returnApply);
	}

	@RequestMapping("getAllReturnApplyAjax")
	@ResponseBody
	public List<ReturnApply> getAllReturnApplyAjax() {

		return returnApplyService.getAllReturnApply();
	}

	@RequestMapping("getReturnApplyByFinishAjax")
	@ResponseBody
	public List<ReturnApply> getReturnApplyByFinishAjax(int finish) {

		return returnApplyService.getReturnApplyByFinish(finish);
	}

	@RequestMapping("getReturnApplyByPrimaryKeyReturnTo")
	public String getReturnApplyByPrimaryKeyReturnTo(int returnApplyId,
			String remark, String returnTo, HttpServletRequest request) {
		request.setAttribute("returnApply",
				returnApplyService.getReturnApplyByPrimaryKey(returnApplyId));
		request.setAttribute(
				"returnApplyMaterialList",
				returnApplyMaterialService
						.getReturnApplyMaterialListByReturnApplyId(returnApplyId));

		request.setAttribute("remark", remark);
		return returnTo;
	}

	@RequestMapping("exameReturnApplyAjax")
	@ResponseBody
	public int exameReturnApplyAjax(int returnApplyId, String exameSuggestion,
			int state, HttpServletRequest request) {
		int userId = ((User) request.getSession().getAttribute("userInfo"))
				.getUserId();
		returnApply = new ReturnApply();
		returnApply.setReturnApplyId(returnApplyId);
		returnApply.setExameById(userId);
		returnApply.setExameTime(TimeUtil.formatTime());
		returnApply.setExameSuggestion(exameSuggestion);
		returnApply.setState(state);
		returnApply.setFinish(1);

		return returnApplyService.exameReturnApply(returnApply);
	}

	/**
	 * 创建退货申请
	 * 
	 * @param supplierId
	 * @param returnTime
	 * @param totalPrice
	 * @param remark
	 * @param returnMaterialList
	 * @return
	 */
	@RequestMapping("createReturnApplyListAjax")
	@ResponseBody
	public int createReturnApplyListAjax(int supplierId, String returnTime,
			Double totalPrice, String remark, String returnMaterialList,
			HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		returnApply = new ReturnApply();
		returnApply.setSupplierId(supplierId);
		returnApply.setReturnTime(returnTime);
		returnApply.setTotalPrice(totalPrice);
		returnApply.setRemark(remark);
		returnApply.setState(1);
		returnApply.setFinish(0);
		returnApply.setCreateById(userId);// 暂时设置1
		returnApply.setCreateTime(TimeUtil.formatTime());
		int result = returnApplyService.createNewReturnApply(returnApply);

		// 处理退货申请的商品list 和出库
		if (!"".equals(returnMaterialList) && returnMaterialList != null
				&& !"[]".equals(returnMaterialList)) {
			int returnApplyId = returnApply.getReturnApplyId();

			ReturnApplyMaterial returnApplyMaterial;
			Stock stock;

			JSONArray list = JSONArray.fromObject(returnMaterialList);
			JSONObject jobj;
			for (int i = 0; i < list.size(); i++) {
				returnApplyMaterial = new ReturnApplyMaterial();
				jobj = (JSONObject) list.get(i);
				returnApplyMaterial.setReturnApplyId(returnApplyId);
				returnApplyMaterial.setMaterialId(jobj.getInt("materialId"));
				returnApplyMaterial.setMaterialQuantity(jobj
						.getDouble("materialQuantity"));
				returnApplyMaterial.setTotalPrice(jobj.getDouble("totalPrice"));
				returnApplyMaterialService
						.createNewReturnApplyMaterial(returnApplyMaterial);

				// 处理出库
				stock = new Stock();
				stock.setMaterialId(jobj.getInt("materialId"));
				stock.setQuantity(0 - jobj.getDouble("materialQuantity"));
				stock.setCreateById(1);
				stock.setCreateTime(TimeUtil.formatTime());
				stock.setState(1);
				stockService.insert(stock);
			}
		}
		return result;
	}

}
